package com.java.client;

import java.io.*;
import java.net.Socket;
import java.util.Properties;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 3000);
        System.out.println("服务器已经连接成功");

        while (true) {
            System.out.println("===== ===== 欢迎来到聊天室 ===== =====");
            System.out.println("1 登录");
            System.out.println("2 注册");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.nextLine();
            switch (choose) {
                case "1" -> login(socket);
                case "2" -> register(socket);
                default -> System.out.println("没有这个选项");
            }
        }
    }

    public static void register(Socket socket) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        while (!checkUsername(username)){
            System.out.println("请重新输入(按t退出)：");
            username = sc.nextLine();
            if("t".equals(username)){
                break;
            }
        }
        if("t".equals(username)){
            return;
        }

        System.out.println("请输入密码：");
        String password = sc.nextLine();
        while (!checkPassword(password)){
            System.out.println("请重新输入(按t退出)：");
            password = sc.nextLine();
            if("t".equals(password)){
                break;
            }
        }
        if("t".equals(password)){
            return;
        }

        System.out.println("请再次输入密码：");
        String rPassword = sc.nextLine();
        while (!checkRPassword(password,rPassword)){
            System.out.println("请重新输入(按t退出)：");
            rPassword = sc.nextLine();
            if("t".equals(rPassword)){
                break;
            }
        }
        if("t".equals(rPassword)){
            return;
        }

        StringBuilder sb = new StringBuilder();
        sb.append("username=").append(username).append("&password=").append(password);

        bw.write("register");
        bw.newLine();
        bw.flush();

        bw.write(sb.toString());
        bw.newLine();
        bw.flush();
    }

    private static boolean checkRPassword(String password,String rPassword) {
        if(!rPassword.equals(password)){
            System.out.println("两次密码不一致");
            return false;
        }
        return true;
    }

    private static boolean checkPassword(String password) {
        //检查密码是否符合规范
        String regex = "[a-zA-Z]{1}\\d{2,7}";
        if(!password.matches(regex)){
            System.out.println("密码首位字母，其余数字，长度3-8");
            return false;
        }
        return true;
    }

    private static boolean checkUsername(String username) throws IOException {
        //检查用户名是否已存在
        FileInputStream fis = new FileInputStream("Info/userInfo.txt");
        Properties properties = new Properties();
        properties.load(fis);
        if(properties.containsKey(username)){
            System.out.println("用户名已存在");
            return false;
        }
        //检查用户名是否符合规范
        String regex = "[a-zA-Z]{6,18}";
        if(!username.matches(regex)){
            System.out.println("用户名为字母，且长度为6-18");
            return false;
        }
        return true;
    }

    public static void login(Socket socket) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();

        StringBuilder sb = new StringBuilder();
        sb.append("username=").append(username).append("&password=").append(password);

        bw.write("login");
        bw.newLine();
        bw.flush();

        bw.write(sb.toString());
        bw.newLine();
        bw.flush();

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String message = br.readLine();
        if ("1".equals(message)) {
            System.out.println("登录成功，开始聊天");
            new Thread(new ClientMyRunnable(socket)).start();
            talk2All(bw);
        } else if ("2".equals(message)) {
            System.out.println("密码输入错误");
        } else if ("3".equals(message)) {
            System.out.println("用户名不存在");
        }
    }

    private static void talk2All(BufferedWriter bw) throws IOException {
        Scanner sc = new Scanner(System.in);
        while (true) {
            String str = sc.nextLine();
            bw.write(str);
            bw.newLine();
            bw.flush();
        }
    }
}

